import {createRouter,createWebHistory} from 'vue-router'
import deaprtment from '@/view/department/department.vue'
import menu from '@/view/menu/menu.vue'
import user from '@/view/user/user.vue'
import login from '@/view/login.vue'
import home from '@/view/home.vue'
// 定义路由集合
const routes = [
	{
		path:'/deaprtment',
		component:deaprtment
	},
	{
		path:'/menu',
		component:menu,
	},
	{
		path:'/user',
		component:user
	},
	{
		path:'/login',
		component:login
	   
	},
	{
		path:'/home',
		component:home
	   
	}
]

const router = createRouter({
	history: createWebHistory(),
	routes,
  });
  
  router.beforeEach((to, from, next) => {
	console.log("前置校验")
	const isLoggedIn = checkIfUserIsLoggedIn(); // 检查用户是否已登录，这里需要根据你的实际情况实现

	if ( !isLoggedIn) {
		console.log("router",router)
		// router.push("/login")
		next(); // 允许访问
	  
	} else {
	  console.log("next")
	  next(); // 允许访问
	}
  });
  
  function checkIfUserIsLoggedIn() {
	// 在这里实现检查用户是否已登录的逻辑，可以是从后端接口获取用户登录状态等
	// 返回 true 表示用户已登录，返回 false 表示用户未登录
    let flag = localStorage.getItem("login") !== null
	console.log(flag)
	 return flag;
  }

export default router